<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of demgtm1</title>
  <meta name="keywords" content="demgtm1">
  <meta name="description" content="DEMGTM1 Demonstrate EM for GTM.">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../../menu.html">Home</a> &gt;  <a href="#">ReBEL-0.2.7</a> &gt; <a href="#">netlab</a> &gt; demgtm1.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../../menu.html"><img alt="<" border="0" src="../../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="menu.html">Index for .\ReBEL-0.2.7\netlab&nbsp;<img alt=">" border="0" src="../../right.png"></a></td></tr></table>-->

<h1>demgtm1
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>DEMGTM1 Demonstrate EM for GTM.</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>This is a script file. </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment">DEMGTM1 Demonstrate EM for GTM.

    Description
     This script demonstrates the use of the EM algorithm to fit a one-
    dimensional GTM to a two-dimensional set of data using maximum
    likelihood. The location and spread of the Gaussian kernels in the
    data space is shown during training.

    See also
    <a href="demgtm2.html" class="code" title="">DEMGTM2</a>, <a href="gtm.html" class="code" title="function net = gtm(dim_latent, nlatent, dim_data, ncentres, rbfunc,prior)">GTM</a>, <a href="gtmem.html" class="code" title="function [net, options, errlog] = gtmem(net, t, options)">GTMEM</a>, <a href="gtmpost.html" class="code" title="function [post, a] = gtmpost(net, data)">GTMPOST</a></pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="gtm.html" class="code" title="function net = gtm(dim_latent, nlatent, dim_data, ncentres, rbfunc,prior)">gtm</a>	GTM	Create a Generative Topographic Map.</li><li><a href="gtmem.html" class="code" title="function [net, options, errlog] = gtmem(net, t, options)">gtmem</a>	GTMEM	EM algorithm for Generative Topographic Mapping.</li><li><a href="gtmfwd.html" class="code" title="function mix = gtmfwd(net)">gtmfwd</a>	GTMFWD	Forward propagation through GTM.</li><li><a href="gtminit.html" class="code" title="function net = gtminit(net, options, data, samp_type, varargin)">gtminit</a>	GTMINIT Initialise the weights and latent sample in a GTM.</li></ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="demnlab.html" class="code" title="function demnlab(action);">demnlab</a>	DEMNLAB A front-end Graphical User Interface to the demos</li></ul>
<!-- crossreference -->


<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <span class="comment">%DEMGTM1 Demonstrate EM for GTM.</span>
0002 <span class="comment">%</span>
0003 <span class="comment">%    Description</span>
0004 <span class="comment">%     This script demonstrates the use of the EM algorithm to fit a one-</span>
0005 <span class="comment">%    dimensional GTM to a two-dimensional set of data using maximum</span>
0006 <span class="comment">%    likelihood. The location and spread of the Gaussian kernels in the</span>
0007 <span class="comment">%    data space is shown during training.</span>
0008 <span class="comment">%</span>
0009 <span class="comment">%    See also</span>
0010 <span class="comment">%    DEMGTM2, GTM, GTMEM, GTMPOST</span>
0011 <span class="comment">%</span>
0012 
0013 <span class="comment">%    Copyright (c) Ian T Nabney (1996-2001)</span>
0014 
0015 <span class="comment">% Demonstrates the GTM with a 2D target space and a 1D latent space.</span>
0016 <span class="comment">%</span>
0017 <span class="comment">%        This script generates a simple data set in 2 dimensions,</span>
0018 <span class="comment">%        with an intrinsic dimensionality of 1, and trains a GTM</span>
0019 <span class="comment">%        with a 1-dimensional latent variable to model this data</span>
0020 <span class="comment">%        set, visually illustrating the training process</span>
0021 <span class="comment">%</span>
0022 <span class="comment">% Synopsis:    gtm_demo</span>
0023 
0024 <span class="comment">% Generate and plot a 2D data set</span>
0025 
0026 data_min = 0.15;
0027 data_max = 3.05;
0028 T = [data_min:0.05:data_max]';
0029 T = [T (T + 1.25*sin(2*T))];
0030 fh1 = figure;
0031 plot(T(:,1), T(:,2), <span class="string">'ro'</span>);
0032 axis([data_min-0.05 data_max+0.05 data_min-0.05 data_max+0.05]);
0033 clc;
0034 disp(<span class="string">'This demonstration shows in detail how the EM algorithm works'</span>)
0035 disp(<span class="string">'for training a GTM with a one dimensional latent space.'</span>)
0036 disp(<span class="string">' '</span>)
0037 fprintf([<span class="keyword">...</span>
0038 <span class="string">'The figure shows data generated by feeding a 1D uniform distribution\n'</span>, <span class="keyword">...</span>
0039 <span class="string">'(on the X-axis) through a non-linear function (y = x + 1.25*sin(2*x))\n'</span>, <span class="keyword">...</span>
0040 <span class="string">'\nPress any key to continue ...\n\n'</span>]);
0041 pause;
0042 
0043 <span class="comment">% Generate a unit circle figure, to be used for plotting</span>
0044 src = [0:(2*pi)/(20-1):2*pi]';
0045 unitC = [sin(src) cos(src)];
0046 
0047 <span class="comment">% Generate and plot (along with the data) an initial GTM model</span>
0048 
0049 clc;
0050 num_latent_points = 20;
0051 num_rbf_centres = 5;
0052 
0053 net = <a href="gtm.html" class="code" title="function net = gtm(dim_latent, nlatent, dim_data, ncentres, rbfunc,prior)">gtm</a>(1, num_latent_points, 2, num_rbf_centres, <span class="string">'gaussian'</span>);
0054 
0055 options = zeros(1, 18);
0056 options(7) = 1;
0057 net = <a href="gtminit.html" class="code" title="function net = gtminit(net, options, data, samp_type, varargin)">gtminit</a>(net, options, T, <span class="string">'regular'</span>, num_latent_points, <span class="keyword">...</span>
0058    num_rbf_centres);
0059 
0060 mix = <a href="gtmfwd.html" class="code" title="function mix = gtmfwd(net)">gtmfwd</a>(net);
0061 <span class="comment">% Replot the figure</span>
0062 hold off;
0063 plot(mix.centres(:,1),  mix.centres(:,2), <span class="string">'g'</span>);
0064 hold on;
0065 <span class="keyword">for</span> i=1:num_latent_points
0066   c = 2*unitC*sqrt(mix.covars(1)) + [ones(20,1)*mix.centres(i,1) <span class="keyword">...</span>
0067       ones(num_latent_points,1)*mix.centres(i,2)];
0068   fill(c(:,1), c(:,2), [0.8 1 0.8]);
0069 <span class="keyword">end</span>
0070 plot(T(:,1), T(:,2), <span class="string">'ro'</span>);
0071 plot(mix.centres(:,1),  mix.centres(:,2), <span class="string">'g+'</span>);
0072 plot(mix.centres(:,1),  mix.centres(:,2), <span class="string">'g'</span>);
0073 axis([data_min-0.05 data_max+0.05 data_min-0.05 data_max+0.05]);
0074 drawnow;
0075 title(<span class="string">'Initial configuration'</span>);
0076 disp(<span class="string">' '</span>)
0077 fprintf([<span class="keyword">...</span>
0078 <span class="string">'The figure shows the starting point for the GTM, before the training.\n'</span>, <span class="keyword">...</span>
0079 <span class="string">'A discrete latent variable distribution of %d points in 1 dimension \n'</span>, <span class="keyword">...</span>
0080 <span class="string">'is mapped to the 1st principal component of the target data by an RBF.\n'</span>, <span class="keyword">...</span>
0081 <span class="string">'with %d basis functions.  Each of the %d points defines the centre of\n'</span>, <span class="keyword">...</span>
0082 <span class="string">'a Gaussian in a Gaussian mixture, marked by the green ''+''-signs.  The\n'</span>, <span class="keyword">...</span>
0083 <span class="string">'mixture components all have equal variance, illustrated by the filled\n'</span>, <span class="keyword">...</span>
0084 <span class="string">'circle around each ''+''-sign, the radii corresponding to 2 standard\n'</span>, <span class="keyword">...</span>
0085 <span class="string">'deviations.  The ''+''-signs are connected with a line according to their\n'</span>, <span class="keyword">...</span>
0086 <span class="string">'corresponding ordering in latent space.\n\n'</span>, <span class="keyword">...</span>
0087 <span class="string">'Press any key to begin training ...\n\n'</span>], num_latent_points, <span class="keyword">...</span>
0088 num_rbf_centres, num_latent_points);
0089 pause;
0090 
0091 figure(fh1);
0092 <span class="comment">%%%% Train the GTM and plot it (along with the data) as training proceeds %%%%</span>
0093 options = foptions;
0094 options(1) = -1;  <span class="comment">% Turn off all warning messages</span>
0095 options(14) = 1;
0096 <span class="keyword">for</span> j = 1:15
0097   [net, options] = <a href="gtmem.html" class="code" title="function [net, options, errlog] = gtmem(net, t, options)">gtmem</a>(net, T, options);
0098   hold off;
0099   mix = <a href="gtmfwd.html" class="code" title="function mix = gtmfwd(net)">gtmfwd</a>(net);
0100   plot(mix.centres(:,1),  mix.centres(:,2), <span class="string">'g'</span>);
0101   hold on;
0102   <span class="keyword">for</span> i=1:20
0103     c = 2*unitC*sqrt(mix.covars(1)) + [ones(20,1)*mix.centres(i,1) <span class="keyword">...</span>
0104     ones(20,1)*mix.centres(i,2)];
0105     fill(c(:,1), c(:,2), [0.8 1.0 0.8]);
0106   <span class="keyword">end</span>
0107   plot(T(:,1), T(:,2), <span class="string">'ro'</span>);
0108   plot(mix.centres(:,1),  mix.centres(:,2), <span class="string">'g+'</span>);
0109   plot(mix.centres(:,1),  mix.centres(:,2), <span class="string">'g'</span>);
0110   axis([0 3.5 0 3.5]);
0111   title([<span class="string">'After '</span>, int2str(j),<span class="string">' iterations of training.'</span>]);
0112   drawnow;
0113   <span class="keyword">if</span> (j == 4)
0114     fprintf([<span class="keyword">...</span>
0115 <span class="string">'The GTM initially adapts relatively quickly - already after \n'</span>, <span class="keyword">...</span>
0116 <span class="string">'4 iterations of training, a rough fit is attained.\n\n'</span>, <span class="keyword">...</span>
0117 <span class="string">'Press any key to continue training ...\n\n'</span>]);
0118 pause;
0119 figure(fh1);
0120   <span class="keyword">elseif</span> (j == 8)
0121     fprintf([<span class="keyword">...</span>
0122 <span class="string">'After another 4 iterations of training:  from now on further \n'</span>, <span class="keyword">...</span>
0123 <span class="string">'training only makes small changes to the mapping, which combined with \n'</span>, <span class="keyword">...</span>
0124 <span class="string">'decrements of the Gaussian mixture variance, optimize the fit in \n'</span>, <span class="keyword">...</span>
0125 <span class="string">'terms of likelihood.\n\n'</span>, <span class="keyword">...</span>
0126 <span class="string">'Press any key to continue training ...\n\n'</span>]);
0127 pause;
0128 figure(fh1);
0129   <span class="keyword">else</span>
0130     pause(1);
0131   <span class="keyword">end</span>
0132 <span class="keyword">end</span>
0133 
0134 clc;
0135 fprintf([<span class="keyword">...</span>
0136 <span class="string">'After 15 iterations of training the GTM can be regarded as converged. \n'</span>, <span class="keyword">...</span>
0137 <span class="string">'Is has been adapted to fit the target data distribution as well \n'</span>, <span class="keyword">...</span>
0138 <span class="string">'as possible, given prior smoothness constraints on the mapping. It \n'</span>, <span class="keyword">...</span>
0139 <span class="string">'captures the fact that the probabilty density is higher at the two \n'</span>, <span class="keyword">...</span>
0140 <span class="string">'bends of the curve, and lower towards its end points.\n\n'</span>]);
0141 disp(<span class="string">' '</span>);
0142 disp(<span class="string">'Press any key to exit.'</span>);
0143 pause;
0144 
0145 close(fh1);
0146 clear all;
0147</pre></div>
<hr><address>Generated on Tue 26-Sep-2006 10:36:21 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>